package com.dongao.activity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.ExpandableListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;

import com.dongao.model.Course;
import com.dongao.model.Exam;
import com.dongao.model.Subject;

public class OnlineCourseActivity extends ExpandableListActivity {

	private ArrayList<Exam> exams;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_online_course);
		
		Intent intent = getIntent();
		exams = (ArrayList<Exam>)intent.getSerializableExtra("exams");
		
		List<Map<String,String>> groups = new ArrayList<Map<String,String>>();
		List<List<Map<String,String>>> childes = new ArrayList<List<Map<String, String>>>();
		
		for (Exam exam : exams) {
			Map<String,String> group = new HashMap<String, String>();
			group.put("group", exam.getExamName());
			groups.add(group);
			
			List<Map<String, String>> child = new ArrayList<Map<String,String>>();
			for (Subject subject : exam.getSubjects()) {
				Map<String,String> childData = new HashMap<String, String>();
				childData.put("child", subject.getNameAndYear());
				child.add(childData);
			}
			childes.add(child);
		}
		
		SimpleExpandableListAdapter sela = new SimpleExpandableListAdapter(
				this, groups, R.layout.group, new String[] { "group" },
				new int[] { R.id.groupTo }, childes, R.layout.child,
				new String[] { "child" }, new int[] { R.id.childTo }
				);
		setListAdapter(sela);
		
	}
	
	@Override
	public boolean onChildClick(ExpandableListView parent, View v,
			int groupPosition, int childPosition, long id) {
		List<Course> courses = exams.get(groupPosition).getSubjects().get(childPosition).getClasses();
		Intent intent = new Intent();
		intent.putExtra("courses", (Serializable)courses);
		intent.setClass(this, SubjectActivity.class);
		OnlineCourseActivity.this.startActivity(intent);
		return super.onChildClick(parent, v, groupPosition, childPosition, id);
	}
	
}
